var layerManager = {
    _map: "",
    _url: [],
    _urlLayer: [],
    _layerName: [],
    _layerObj: [],
    _currentLayer: 0,
    _baseLayer:[]
};

var overLayer = {
    _url: [],
    _urlLayer: [],
    _layerObj: []
};
/**
 * 设备layer的地图目标
 * @param map{object} 地图对象
 */
layerManager.setMap = function (map) {
    this._map = map;
};
/**
 * 添加图层
 * @param url {string}
 * @param urlLayer {string}
 * @param name {string}
 */
layerManager.addLayer = function (url, urlLayer, name) {
    this._url.push(url);
    this._urlLayer.push(urlLayer);
    this._layerName.push(name);
};

/**
 * 添加叠加图层
 * @param url
 * @param urlLayer
 * @param name
 */
layerManager.addOverLayer = function (url, urlLayer) {
    overLayer._url.push(url);
    overLayer._urlLayer.push(urlLayer);
};
/**
 * 清空所有图层
 * @param layerObj
 */
layerManager.emptyMap = function (layerObj) {
    for (var i = 0; i < this._layerObj.length; i++) {
        this._map.removeLayer(this._layerObj[i]);
    }
    this._layerObj = [];
    //移除叠加图层
    for (var j = 0; j < overLayer._layerObj.length; j++) {
        this._map.removeLayer(overLayer._layerObj[j]);
    }
    overLayer._layerObj = [];
};

/**
 * 添加指定图层
 * @param i{number}
 */
layerManager.createLayer = function (i) {
    this._currentLayer = i;
    var wmsLayer = L.tileLayer.wms(this._url[i], {
        layers: this._urlLayer[i],
        crs: L.CRS.EPSG4326,
        format: 'image/png',
        transparent: true,
        maxZoom: 33,
        minZoom: 14
    }).addTo(this._map);
    // wmsLayer.addTo(this._map);
    this._layerObj.push(wmsLayer);
};

layerManager.createOverLayer = function () {
    for (var i = 0; i < overLayer._url.length; i++) {
        var wmsLayer = L.tileLayer.wms(overLayer._url[i], {
            layers: overLayer._urlLayer[i],
            crs: L.CRS.EPSG4326,
            format: 'image/png',
            transparent: true,
            maxZoom: 33,
            minZoom: 1
        });
        wmsLayer.addTo(this._map);
        overLayer._layerObj.push(wmsLayer);
    }
};
/**
 * 添加所有图层
 */
layerManager.createAllLayer = function () {
    for (var i = 0; i < this._url.length; i++) {
        var wmsLayer = L.tileLayer.wms(this._url[i], {
            layers: this._urlLayer[i],
            crs: L.CRS.EPSG4326,
            format: 'image/png',
            transparent: true,
            maxZoom: 33,
            minZoom: 14
        });
        wmsLayer.addTo(this._map);
        this._layerObj.push(wmsLayer);
    }
};
/**
 * 切换到指定的楼层号
 * @param floorNumber{number}楼层号
 */
layerManager.switchLayer = function (floorNumber) {
    this.emptyMap();
    this.createLayer(floorNumber);
    this.createOverLayer();
    console.log("切换到" + floorNumber);
};
/**
 * 移除当前图层
 */
layerManager.removeCurrentLayer = function () {
    this.emptyMap();
    this.createOverLayer();
};
/**
 * 恢复上一个图层
 */
layerManager.recoverCurrentLayer = function () {
    this.createLayer(this._currentLayer);
};
/**
 * 更换底图
 * @param layer
 */
layerManager.setBaseLayer=function (url,layerID,token) {
    if(this._baseLayer.length!=0){
        this._map.removeLayer(this._baseLayer[0]);
    }
    this.emptyMap();
    var layer1 = L.tileLayer(url, {
        maxZoom: 33,
        id: layerID,
        accessToken: token
    });
    this._map.addLayer(layer1);
    this._baseLayer.push(layer1);
    this.createOverLayer();
};

layerManager.getCurrentFloor=function () {
    return this._currentLayer+1;
};


